package com.xr.web.controller.miniprogram;

import com.xr.common.annotation.Log;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramStandards;
import com.xr.miniprogram.domain.vo.MiniProgramStandardsVO;
import com.xr.miniprogram.service.IMiniProgramStandardsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * description: 服务模块-规格接口
 *
 * @author: wls
 * @date: 2023/11/9 9:37
 * @version:1.0
 */
@Api("服务模块-规格")
@RestController
@RequestMapping("/mini/standards")
public class MpStandardsController extends BaseController {

    @Resource
    public IMiniProgramStandardsService miniProgramStandardsService;

    /**
     * 根据服务详情id获取服务规格列表
     * @param serviceId
     * @return list
     */
    @ApiOperation("根据服务详情id获取服务规格列表")
    @PreAuthorize("@ss.hasPermi('mini:standards:list')")
    @GetMapping(value = "/list")
    public AjaxResult list(@ApiParam(value = "服务id", required = true) @RequestParam("serviceId") Long serviceId)
    {
        return success(MiniProgramStandardsVO.toMiniProgramStandardsVOList(miniProgramStandardsService.getStandardsListByServiceId(serviceId)));
    }

    /**
     * 新增服务规格
     * @param miniProgramStandardsVO
     * @return
     */
    @ApiOperation("新增服务规格")
    @PreAuthorize("@ss.hasPermi('mini:standards:add')")
    @Log(title = "服务规格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MiniProgramStandardsVO miniProgramStandardsVO){
        MiniProgramStandards miniProgramStandards = miniProgramStandardsVO.toMiniProgramStandards();
        miniProgramStandards.setCreateBy(getUsername());
        miniProgramStandards.setCreateTime(new Date());
        miniProgramStandards.setStandardsVersion(1L);
        return success(miniProgramStandardsService.addStandards(miniProgramStandards));
    }

    /**
     * 修改服务规格
     * @param miniProgramStandardsVO
     * @return
     */
    @ApiOperation("修改服务规格")
    @PreAuthorize("@ss.hasPermi('mini:standards:edit')")
    @Log(title = "服务规格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MiniProgramStandardsVO miniProgramStandardsVO){
        MiniProgramStandards miniProgramStandards = miniProgramStandardsVO.toMiniProgramStandards();
        miniProgramStandards.setUpdateBy(getUsername());
        miniProgramStandards.setUpdateTime(new Date());
        miniProgramStandards.setStandardsVersion(miniProgramStandards.getStandardsVersion() + 1);
        return success(miniProgramStandardsService.updateStandards(miniProgramStandards));
    }

    /**
     * 删除服务规格
     * @param serviceStandardsId
     * @return
     */
    @ApiOperation("删除服务规格")
    @PreAuthorize("@ss.hasPermi('mini:standards:remove')")
    @Log(title = "服务规格", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam("serviceStandardsId") Long serviceStandardsId ){
        return success(miniProgramStandardsService.deleteStandards(serviceStandardsId));
    }
}
